version: "3.9"

x-media-volume: &media-volume ./media/:${PROJECT_PATH:-/usr/src/app}/media/

services:
  tg_bot:
    container_name: dc_shop.tg_bot
    image: "${BOT_IMAGE_NAME:-dc_shop.dc_shop}"
    build:
      context: .
    restart: unless-stopped
    stop_signal: SIGINT
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - *media-volume
    networks:
      - dc_shop.postgres.network
      - dc_shop.redis.network
    environment:
      - TZ=Asia/Bangkok
    env_file:
      - ".env"
    command: [ "python", "-m", "dc_shop.tg_bot" ]
  api:
    container_name: dc_shop.api
    image: "${BOT_IMAGE_NAME:-dc_shop.api}"
    build:
      context: .
    restart: unless-stopped
    expose:
      - "5005"
    ports:
      - "5005:5005"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - *media-volume
    networks:
      - dc_shop.postgres.network
      - dc_shop.redis.network
    environment:
      - TZ=Asia/Bangkok
    env_file:
      - ".env"
    command: ["uvicorn", "dc_shop.webapp.wsgi:app", "--workers", "4", "--host", "0.0.0.0", "--port", "5005"]
    healthcheck:
      test: [ "CMD-SHELL", "curl -fsSL http://localhost:5005/healthcheck/" ]
      interval: 10s
      timeout: 60s
      retries: 5
      start_period: 10s
  admin_panel:
    container_name: dc_shop.admin_panel
    image: "${BOT_IMAGE_NAME:-dc_shop.admin_panel}"
    build:
      context: .
    restart: unless-stopped
    stop_signal: SIGINT
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "5000:5000"
    volumes:
      - *media-volume
    networks:
      - dc_shop.postgres.network
    env_file:
      - ".env"
    command: [ "gunicorn", "dc_shop.admin_panel.wsgi:app", "--workers", "2", "--threads", "2", "-b", "0.0.0.0:5000" ]
  worker:
    container_name: dc_shop.worker
    image: "${BOT_IMAGE_NAME:-dc_shop.worker}"
    build:
      context: .
    restart: unless-stopped
    stop_signal: SIGINT
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - *media-volume
    networks:
      - dc_shop.postgres.network
      - dc_shop.redis.network
    environment:
      - TZ=Asia/Bangkok
    env_file:
      - ".env"
    command: [ "arq", "dc_shop.worker.main.WorkerSettings" ]
  redis:
    container_name: dc_shop.redis
    image: redis:7.0.10-alpine
    command:
      sh -c '
      redis-server
      --appendonly yes
      --bind 0.0.0.0'
    volumes:
      - dc_shop.redis.data:/data:rw
    ports:
      - "6380:6379"
    networks:
      - dc_shop.redis.network
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]
      interval: 10s
      timeout: 60s
      retries: 5
      start_period: 10s
    environment:
      - TZ=Asia/Bangkok
    env_file:
      - ".env"
  postgres_migration:
    image: "${BOT_IMAGE_NAME:-dc_shop.dc_shop}"
    container_name: dc_shop.postgres_migration
    build:
      context: .
    restart: on-failure
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - dc_shop.postgres.network
    volumes:
      - .:${PROJECT_PATH:-/usr/src/app}
      - ./alembic.ini:${PROJECT_PATH:-/usr/src/app}/alembic.ini:ro
    env_file:
      - ".env"
    command: [ "python", "-m", "alembic", "upgrade", "head" ]
  postgres:
    container_name: dc_shop.postgres
    image: "postgres:15-alpine"
    hostname: dc_shop.postgres
    restart: unless-stopped
    expose:
      - "${DB__PORT:-5432}"
    ports:
      - "${DB__PORT:-5432}:5432"
    networks:
      - dc_shop.postgres.network
    environment:
      POSTGRES_PASSWORD: $DB__PASSWORD
      POSTGRES_USER: $DB__USER
      POSTGRES_DB: $DB__NAME
      TZ: Asia/Bangkok
      PGTZ: Asia/Bangkok
    volumes:
      - dc_shop.postgres.data:/var/lib/postgresql/users:rw
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
      interval: 10s
      timeout: 60s
      retries: 5
      start_period: 10s
    env_file:
      - ".env"
  postgres_backup:
    container_name: dc_shop.postgres_backup
    image: prodrigestivill/postgres-backup-local:15-alpine
    networks:
      - dc_shop.postgres.network
    environment:
      POSTGRES_HOST: dc_shop.postgres
      POSTGRES_PASSWORD: $DB__PASSWORD
      POSTGRES_USER: $DB__USER
      POSTGRES_DB: $DB__NAME
      BACKUP_DIR: /backups
      POSTGRES_EXTRA_OPTS: -Z6 --schema=public -b -a
      SCHEDULE: ${POSTGRES_BACKUP_SCHEDULE:-@daily}
      HEALTHCHECK_PORT: 8080
      TZ: Asia/Bangkok
    volumes:
      - ${POSTGRES_BACKUP_DIR:-./.backups/postgres}:/backups
    env_file:
      - ".env"
  pgadmin:
    container_name: dc_shop.pgadmin
    image: dpage/pgadmin4:latest
    restart: unless-stopped
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-admin@example.com}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
      PGADMIN_LISTEN_PORT: 5050
    volumes:
      - dc_shop.pgadmin.data:/var/lib/pgadmin
    ports:
      - "${PGADMIN_LISTEN_PORT:-5050}:5050"
    expose:
      - "5050"
    networks:
      - dc_shop.postgres.network
    depends_on:
      postgres:
        condition: service_healthy
    env_file:
      - ".env"

volumes:
  dc_shop.postgres.data: {}
  dc_shop.redis.data: {}
  dc_shop.pgadmin.data: {}

networks:
  dc_shop.postgres.network: {}
  dc_shop.redis.network: {}
